#include "../h/apue.h"
#include <errno.h>
#include <limits.h>

#ifdef OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

#define OPEN_MAX_GUESS 256

int main(void)
{
    long  max = open_max();
    printf("_SC_VERSION:%ld.\n", sysconf(_SC_VERSION));
    printf("_SC_XOPEN_VERSION:%ld.\n", sysconf(_SC_XOPEN_VERSION));

    printf("_POSIX_ASYNCHRONOUS_IO:%ld\n", _POSIX_ASYNCHRONOUS_IO);
    printf("sysconf(_POSIX_ASYNCHRONOUS_IO):%ld\n\n", sysconf(_SC_ASYNCHRONOUS_IO));

    printf("_POSIX_BARRIERS:%ld\n", _POSIX_BARRIERS);
    printf("sysconf(_POSIX_BARRIERS):%ld\n\n", sysconf(_SC_BARRIERS));

    printf("_POSIX_MAPPED_FILES:%ld\n", _POSIX_MAPPED_FILES);
    printf("sysconf(_POSIX_MAPPED_FILES):%ld\n\n", sysconf(_SC_MAPPED_FILES));

    printf("_POSIX_TIMERS:%ld\n", _POSIX_TIMERS);
    printf("sysconf(_POSIX_TIMERS):%ld\n\n", sysconf(_SC_TIMERS));

    printf("sysconf(_PC_2_SYMLINKS):%ld\n\n", pathconf("/home/guopong/", _PC_2_SYMLINKS));

    printf("open max: %ld.\n", max);
}

long open_max(void)
{
    if (openmax == 0)
    {
        errno = 0;
        if ((openmax = sysconf(_SC_OPEN_MAX)) < 0)
        {
            if (errno == 0)
                openmax = OPEN_MAX_GUESS;
            else
                err_sys("sysconf error for _SC_OPEN_MAX");
        }
    }

    return openmax;
}
